package com.compty.paint.manager;

import java.util.Date;
import java.util.List;

import com.compty.core.framework.util.DateUtil;
import com.compty.core.hibernate.HibernateEntityDao;
import com.compty.paint.domain.SmsReport;

/**
 * sms短信发送报告记录manager
 * @author 朱庆辉
 */
public class SmsReportManager  extends HibernateEntityDao<SmsReport>{

	/**
	 * 检查电话号码及ip是否已注册或者超过最大获取激活码次数
	 * @param sMobile 电话号码
	 * @param clientIp 客户端ip
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String checkByMobileAndIp(String sMobile,String clientIp) {
		
		if(sMobile == null){
			return null;
		}
		
		String findUser = "from CustomerInfo where userName = ?";
		List customerList = super.find(findUser, sMobile);
		if( customerList != null && customerList.size() > 0 ){
//			request.setAttribute("errormessge", "该号码已注册。");
			if(!"18925210869".equals(sMobile)){
				return "2";
			}
		}
		
		long beginTime = getBeginTime();
		long endTime = DateUtil.getNow().getTime();
		
		String checkByIp = "from SmsReport where clientIp = ? and createTime>? and createTime <= ?";
		List checkByIplist = super.find(checkByIp, sMobile,beginTime,endTime);
		if( checkByIplist != null && checkByIplist.size() > 3 ){
//			request.setAttribute("errormessge", "同一IP今日只允许获取短信激活码三次，您已超过最大使用次数。");
			return "3";
		}
		
		String checkByMobile = "from SmsReport where mobile = ? and createTime>? and createTime <=?";
		List<SmsReport> checkByMobileList  = super.find(checkByMobile, sMobile,beginTime,endTime);
		if( checkByMobileList != null && checkByMobileList.size() > 3 ){
//			request.setAttribute("errormessge", "该号码今日最多能获取三次激活码，您已超过最大使用次数。");
			return "4";
		}
		
		return "1";
	}
	
	private long getBeginTime(){
		String newDateStr = DateUtil.toString(DateUtil.getNow(), "yyyy-MM-dd");
		Date  newDate = DateUtil.toDate(newDateStr, "yyyy-MM-dd");
		return newDate.getTime();
	}
}
